package openase.audio;

import java.nio.ByteOrder;

import javax.sound.sampled.AudioFormat.Encoding;

public class AudioUtils {

    public static AudioFormat getRecordingFormat() {
        float sampleRate = 44100;
        int sampleSizeInBits = 8;
        int channels = 1; // mono
        boolean signed = true;
        ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
        
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, byteOrder);
    }
    
    public static javax.sound.sampled.AudioFormat asJavaSoundAudioFormat(AudioFormat format) {
        float sampleRate = format.getSampleRate();
        int sampleSizeInBits = format.getSampleSizeInBits();
        int channels = format.getChannelCount();
        boolean signed = format.isSigned();
        boolean bigEndian = format.getByteOrder() == ByteOrder.BIG_ENDIAN;

        return new javax.sound.sampled.AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    }

    public static AudioFormat fromJavaSoundAudioFormat(javax.sound.sampled.AudioFormat format) {
        float sampleRate = format.getSampleRate();
        int sampleSizeInBits = format.getSampleSizeInBits();
        int channels = format.getChannels();
        boolean signed = format.getEncoding() == Encoding.PCM_SIGNED;
        boolean bigEndian = format.isBigEndian();
        
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
    }

}
